package com.fin.zw.aiqas.service;

import com.fin.zw.aiqas.entity.UserInfo;
import com.fin.zw.aiqas.enums.Whether;
import com.fin.zw.aiqas.model.response.RoleInfoResponse;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.repository.UserInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.stream.Collectors;

@Service("commonService")
public class CommonService {


    @Autowired
    private UserInfoRepository userInfoRepository;

    /**
     * 判断当前登录人是否是管理员（包含多种角色）
     * @param sessionModel
     * @return
     */
    public boolean whetherAdmin(SessionModel sessionModel){
        boolean result = false;
        Set<RoleInfoResponse> set = sessionModel.getRoleInfoSet();
        set.stream().filter(role -> role.getWhether().equals(Whether.YES)).collect(Collectors.toSet());
        if (set != null && !set.isEmpty()) {
            result = true;
        }
        return result;
    }



    /**
     * 获取当前登录人用户信息
     * @param sessionModel
     * @return
     */
    public UserInfo getUserInfo(SessionModel sessionModel){
        if (sessionModel.getUserId() != null) {
            return userInfoRepository.findById(sessionModel.getUserId()).orElse(new UserInfo());
        }
        return null;
    }

}
